package com.c201801020208.ybl.Company;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.c201801020208.ybl.Company.Activity.AddJobActivity;
import com.c201801020208.ybl.Company.Activity.JobInfoActivity;
import com.c201801020208.ybl.R;
import com.c201801020208.ybl.Tables.CompanyInfo;
import com.c201801020208.ybl.User.Activity.jianliActivity;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

public class HomePageFragment extends Fragment {
    String username = null;
    RecyclerView mRecyclerView;
    List<CompanyInfo> mList = new ArrayList<>();
    ImageView imageView;
    HomePageAdapter mMyAdapter = new HomePageAdapter();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_page, container, false);
        mRecyclerView = view.findViewById(R.id.recycle1);
        return view;
    }

    //刷新
    @Override
    public void onResume() {
        super.onResume();
        mList.removeAll(mList);
        BmobQuery<CompanyInfo> categoryBmobQuery = new BmobQuery<>();
        categoryBmobQuery.addWhereEqualTo("username", username);
        categoryBmobQuery.findObjects(new FindListener<CompanyInfo>() {
            @Override
            public void done(List<CompanyInfo> object, BmobException e) {
                if (e == null) {
//                    Toast.makeText(getContext(), "查询成功：" + object.size(), Toast.LENGTH_LONG).show();
                    for (int i = 0; i < object.size(); i++) {
                        CompanyInfo companyInfo = new CompanyInfo();
                        companyInfo.setUsername(object.get(i).getUsername());
                        companyInfo.setAddress(object.get(i).getAddress());
                        companyInfo.setWorktime(object.get(i).getWorktime());
                        companyInfo.setWorkexperience(object.get(i).getWorkexperience());
                        companyInfo.setCompanyname(object.get(i).getCompanyname());
                        companyInfo.setMoney(object.get(i).getMoney());
                        companyInfo.setType(object.get(i).getType());
                        companyInfo.setJobname(object.get(i).getJobname());
                        companyInfo.setEducation(object.get(i).getEducation());
                        companyInfo.setDescribe(object.get(i).getDescribe());
                        companyInfo.setObjectId(object.get(i).getObjectId());
                        Log.e("BMOB", object.get(i).getObjectId());
                        mList.add(companyInfo);
                    }
                    mMyAdapter.notifyDataSetChanged();
                } else {
                    Log.e("BMOB", e.toString());
//                    Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        username = getActivity().getIntent().getStringExtra("username");

        //跳转
        imageView = getActivity().findViewById(R.id.add);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AddJobActivity.class);
                intent.putExtra("username", username);
                startActivity(intent);
            }
        });

        mRecyclerView = getActivity().findViewById(R.id.recycle1);
        mRecyclerView.setAdapter(mMyAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    class HomePageAdapter extends RecyclerView.Adapter<HomePageAdapter.MyViewHoder> {

        @NonNull
        @Override
        public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.chatlayout, parent, false);
            MyViewHoder myViewHoder = new MyViewHoder(view);
            return myViewHoder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHoder holder, int position) {
            CompanyInfo companyInfo = mList.get(position);
            holder.address.setText(companyInfo.getAddress());
            holder.companyname.setText(companyInfo.getCompanyname());
            holder.jobname.setText(companyInfo.getJobname());
            holder.education.setText(companyInfo.getEducation());
            holder.workexperience.setText(companyInfo.getWorkexperience());
            holder.content.setText(companyInfo.getDescribe());
            holder.salary.setText(companyInfo.getMoney());
            holder.worktime.setText(companyInfo.getWorktime());
            holder.jobtype.setText(companyInfo.getType());
            holder.username.setText(companyInfo.getUsername());
//            holder.myrootview.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Intent intent = new Intent(getActivity(), AddJobActivity.class);
//                    intent.putExtra("companyInfo",companyInfo);
//                    startActivity(intent);
//                }
//            });
            class MyRunnable implements Runnable {
                @Override
                public void run() {
                    //TODO...
                    companyInfo.delete(new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e == null) {
//                                Toast.makeText(getContext(), companyInfo.getUpdatedAt()+"cg", Toast.LENGTH_LONG).show();
                            } else {
//                                Toast.makeText(getContext(), e.getMessage()+"sb", Toast.LENGTH_LONG).show();
                            }
                        }
                    });
                }
            }
            holder.myrootview.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {

                    new AlertDialog.Builder(v.getContext())
                            .setMessage("确定删除这条招聘信息吗？")
                            .setCancelable(false)
                            .setPositiveButton("是", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
//                                    Toast.makeText(getContext(), "id"+companyInfo.getObjectId(), Toast.LENGTH_LONG).show();
                                    mList.remove(position);
                                    // 调用使用线程
                                    Runnable myRunnable = new MyRunnable();         // 创建Runable类对象
                                    Thread thread = new Thread(myRunnable);         // 将Runnable对象作为Thread target创建新的线程
                                    thread.start();
                                    mMyAdapter.notifyDataSetChanged();
                                }
                            })
                            .setNegativeButton("否", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).create().show();
                    return false;
                }
            });
//            mMyAdapter.notifyDataSetChanged();
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }

        class MyViewHoder extends RecyclerView.ViewHolder {
            TextView jobname;
            TextView companyname;
            TextView salary;
            TextView username;
            TextView address;
            TextView worktime;
            TextView jobtype;
            TextView education;
            TextView workexperience;
            TextView content;
            CardView myrootview;

            public MyViewHoder(@NonNull View itemView) {
                super(itemView);
                companyname = itemView.findViewById(R.id.textView13);
                salary = itemView.findViewById(R.id.textView14);
                username = itemView.findViewById(R.id.textView7);
                address = itemView.findViewById(R.id.textView8);
                worktime = itemView.findViewById(R.id.textView19);
                jobtype = itemView.findViewById(R.id.textView16);
                content = itemView.findViewById(R.id.textView17);
                workexperience = itemView.findViewById(R.id.textView18);
                education = itemView.findViewById(R.id.textView20);
                myrootview = itemView.findViewById(R.id.rootview);
                jobname = itemView.findViewById(R.id.textView9);
            }
        }
    }

}
